﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class SupplierService
    {
        /// <summary>
        /// 获取供货商信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="supplierno"></param>
        /// <returns></returns>
        public Supplier GetSupplier(long id, String supplierno)
        {
            Supplier supplier = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    supplier = new Supplier();
                    supplier.Id = id;
                    supplier.Supplierno = supplierno;
                    supplier = new SupplierDAO().GetSupplier(conn, supplier);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return supplier;
        }

        /// <summary>
        /// 获取供货商信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetSupplierTable(int pageindex, int pagesize, String supplierno, String shortname, 
            String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Supplier supplier = new Supplier();
                    supplier.Pageindex = pageindex;
                    supplier.Pagesize = pagesize;
                    supplier.Supplierno = supplierno;
                    supplier.Shortname = shortname;
                    supplier.Ascfield = ascfield;
                    supplier.Descfield = descfield;
                    dt = new SupplierDAO().GetSupplierTable(conn, supplier);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询供货商记录数
        /// </summary>
        /// <returns></returns>
        public long GetSupplierRecord(String supplierno, String shortname)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Supplier supplier = new Supplier();
                    supplier.Supplierno = supplierno;
                    supplier.Shortname = shortname;
                    record = new SupplierDAO().GetSupplierRecord(conn, supplier);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入供货商信息
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public long InsertSupplier(Supplier supplier)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new SupplierDAO().InsertSupplier(conn, supplier);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新供货商信息
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public int UpdateSupplier(Supplier supplier)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new SupplierDAO().UpdateSupplier(conn, supplier);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteSupplier(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        SupplierDAO supplierDAO = new SupplierDAO();
                        Supplier supplier = new Supplier();
                        int flag = 0;
                        foreach (String lopid in arr)
                        {
                            supplier.Id = Int64.Parse(lopid);
                            flag = supplierDAO.DeleteSupplier(conn, supplier);
                            if (flag > 0)
                                count += 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
